package main

import (
	"bytes"
	"fmt"
	"gopkg.in/gomail.v2"
	"html/template"
	"log"
)

type UserData struct {
	Name     string
	OrderID  string
	Amount   float64
	Product  string
	DateTime string
}

func main1111() {

	// 配置QQ邮箱SMTP信息
	smtpHost := "smtp.163.com"     // SMTP服务器地址
	smtpPort := 25                 // SMTP端口（QQ邮箱使用587端口）
	sender := "yunets@163.com"     // 发件人QQ邮箱地址
	password := "JLfRQBJXF6pNDbJC" // 授权码，不是QQ密码（需在QQ邮箱设置中获取）
	receiver := "yunets@163.com"   // 收件人邮箱地址

	// 创建邮件消息
	m := gomail.NewMessage()
	m.SetHeader("From", sender)
	m.SetHeader("To", receiver)
	m.SetHeader("Subject", "密码重置")

	// 构建HTML邮件内容
	htmlTemplate := `
	<!DOCTYPE html>
	<html>
	<head>
		<meta charset="UTF-8">
		<title>密码重置</title>
	</head>
	<body>
		<h1>密码重置</h1>
		<p>点击进行<a href="https://www.example.com">官方网站</a>。</p>
        <table border="1" cellpadding="10" style="border-collapse: collapse;">
            <tr>
                <th>订单编号</th>
                <th>产品名称</th>
                <th>金额</th>
                <th>下单时间</th>
            </tr>
            <tr>
                <td>{{.OrderID}}</td>
                <td>{{.Product}}</td>
                <td>¥{{printf "%.2f" .Amount}}</td>
                <td>{{.DateTime}}</td>
            </tr>
        </table>
	</body>
	</html>
	`
	tmpl, err := template.New("email_template").Parse(htmlTemplate)

	if err != nil {
		log.Fatalf("解析模板失败: %v", err)
	}

	templatePath := "D:\\person_management\\zs_work\\sso\\template.html"
	tmpl, err = template.ParseFiles(templatePath)

	userData := UserData{
		Name:     "张三",
		OrderID:  "ORD20230001",
		Amount:   199.99,
		Product:  "Go语言高级编程",
		DateTime: "2023-05-20 14:30:00",
	}

	var htmlBody bytes.Buffer
	err = tmpl.Execute(&htmlBody, userData)
	if err != nil {
		log.Fatalf("填充模板数据失败: %v", err)
	}

	// 添加HTML邮件体
	m.SetBody("text/html", htmlBody.String())

	// 创建SMTP客户端
	d := gomail.NewDialer(smtpHost, smtpPort, sender, password)

	// 发送邮件
	if err := d.DialAndSend(m); err != nil {
		log.Fatal("发送邮件失败:", err)
	}

	fmt.Println("邮件发送成功！")
}

func main() {
	templatePath := "D:\\person_management\\zs_work\\sso\\template.html"
	tmpl, err := template.ParseFiles(templatePath)
	if err != nil {
	}

	userData := struct {
		Name string
		Age  int
	}{"张三", 20}

	var htmlBody bytes.Buffer
	err = tmpl.Execute(&htmlBody, userData)
	if err != nil {
		log.Fatalf("填充模板数据失败: %v", err)
	}
	fmt.Println(htmlBody.String())
}
